package org.roger.search;

import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;

/**
 * 短语搜索
 * @param field		需要进行查询的域
 * @param phrase	短语数组（短语的前后顺序可变，这个是和SpanNearQuery最重要的区别）
 * @param slop		匹配短语需要的步长
 * @param indexPath
 */
public class PhraseSearch extends LuceneSearchBase{
	
	 private String field;
	 private String[] phrase;
	 private int slop;
	 
	 public PhraseSearch(String field,String[] phrase,int slop){
		 this.field = field;
		 this.phrase = phrase;
		 this.slop = slop;
	 }

	@Override
	public Query getSearchQuery() {
		PhraseQuery query = new PhraseQuery();
		query.setSlop(slop);
		for(String word : phrase){
			query.add(new Term(field,word));
		}
		return query;
	}
	
	public static void main(String[] args) {
		String[] phrase = {"emotions","freshness"};
		PhraseSearch search = new PhraseSearch("contents",phrase, 3);
		try {
			search.search("D:/index");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}
}
